package com.medata.controller;

import com.medata.dataObject.NewsInfo;
import com.medata.service.DatabaselistService;
import com.medata.service.NewsInfoService;
import com.medata.utils.ResultVOUtil;
import com.medata.utils.TimeUtil;
import com.medata.vo.ResultVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.persistence.EntityManager;
import java.sql.Timestamp;

/**
 * @author ：zb
 * @date ：Created in 2019/12/31 0031 9:24
 * @description：
 */
@Controller
@RequestMapping("editor")
public class Editor2Controller {
    @Autowired
    private DatabaselistService databaselistService;
    @Autowired
    private NewsInfoService newsInfoService;
    @Autowired
    private EntityManager entityManager;

    @RequestMapping(value = "/newsManager",method = RequestMethod.GET)
    public String newsManager(){
        return "/newsManager";
    }

    @RequestMapping(value = "/newsPut",method = RequestMethod.POST)
    @ResponseBody
    public ResultVo newsPut(String newsId,String title,String language,String summernote){
        NewsInfo info=new NewsInfo();
        if (!newsId.isEmpty()){
            info = newsInfoService.findByNewId(Integer.valueOf(newsId));
        }else{
            info.setType(1);
        }
        info.setTime(Timestamp.valueOf(TimeUtil.getCurDate()));
        info.setTitle(title);
        info.setContent(summernote);
        info.setLanguagetype(language);
        newsInfoService.saveOrUpdate(info);
        return ResultVOUtil.success();
    }

    @RequestMapping(value = "/deleteNew",method = RequestMethod.POST)
    @ResponseBody
    public ResultVo deleteNew(Integer newsId){
        newsInfoService.deleteByNewId(newsId);
        return ResultVOUtil.success();
    }
}
